<?php
    error_reporting(0);
    define('__ROOT__', dirname(dirname(__FILE__)));  
    require_once(__ROOT__.'\config\config.php'); 
    require_once(__ROOT__.'\model\db.php');
    require_once(__ROOT__.'\model\products.php');
    require_once(__ROOT__.'\model\categories.php');
    $product = new Products();
    $category = new Categories();
    if(isset($_POST['add']))
    {
        $image = $_FILES['image']['name'];
        $name = $_POST['name'];
        $price = $_POST['price'];
        $category = $_POST['category'];
        $quantity = $_POST['quantity'];
        if($image == null || $name == null || $price == null || $category == null || $quantity == null)
            echo("<script>location.href = '".HOST."view/view_addproducts.php?action=fail';</script>");
        else {
            if($price <= 0 || $quantity < 0)
                echo("<script>location.href = '".HOST."view/view_addproducts.php?action=fail';</script>");
            else
            {
                if(strlen($name) <= 50)
                {
                    $products = $product->insert($name, $price, $category, $quantity, $image);
                    echo("<script>location.href = '".HOST."view/view_addproducts.php?action=succeed';</script>");
                }
                else
                {
                    echo("<script>location.href = '".HOST."view/view_addproducts.php?action=fail';</script>");
                }
            }
        }
    }
    if(isset($_POST['delete_x']))
    {
        $products = $product->delete($_GET['id']);
        echo("<script>location.href = '".HOST."view/view_adminproduct.php?action=succeed';</script>");
    }
    if(isset($_POST['edit_x']))
    {
        $useragent = $_SERVER['HTTP_USER_AGENT'];
        $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
        session_write_close();
        $products = $product->selectid($_GET['id']);
        $categories = $category->get_name_by($products[0]['category']);
        $post = array(
            'id' => $products[0]['id'],
            'name' => $products[0]['name'],
            'price' => $products[0]['price'],
            'category' => $categories['name'],
            'quantity' => $products[0]['quantity'],
            'image' => $products[0]['image']
        );
        $url = HOST.'view/view_updateproduct.php';
        session_write_close();
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, count($post));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;
    }
    if(isset($_POST['update']))
    {
        $image = $_FILES['image']['name'];
        $name = $_POST['name'];
        $price = $_POST['price'];
        $category = $_POST['category'];
        $quantity = $_POST['quantity'];
        if($name == null || $price == null || $category == null || $quantity == null)
            echo("<script>location.href = '".HOST."view/view_adminproduct.php?action=fail';</script>");
        else {
            if($price <= 0 || $quantity < 0)
                echo("<script>location.href = '".HOST."view/view_adminproduct.php?action=fail';</script>");
            else
            {      
                if($image == null)
                {
                    $products = $product->update($_GET['id'], $name, $price, $category, $quantity, $_GET['image']);
                    echo("<script>location.href = '".HOST."view/view_adminproduct.php?action=succeed';</script>");
                }
                else
                {
                    $products = $product->update($_GET['id'], $name, $price, $category, $quantity, $image);
                    echo("<script>location.href = '".HOST."view/view_adminproduct.php?action=succeed';</script>");
                }                
            }
        }
    }
?>